package com.xqyq.controller.manage;

import java.sql.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.xqyq.model.Match;
import com.xqyq.model.MatchContent;
import com.xqyq.model.Prize;
import com.xqyq.service.FileService;
import com.xqyq.service.MatchService;
import com.xqyq.util.CommonUtil;

@RequestMapping("/manage")
@Controller
public class SysMatchController {

	@Autowired
	private MatchService matchService;
	
	@Autowired
	private FileService fileService;

	// @Autowired
	// private PrizeService prizeService;

	@RequestMapping("/match_edit.html")
	public String matchEdit(HttpServletRequest request) {
		if (request.getParameter("match_id") != null) {
			Match match = matchService.getTheWholeMatch(Integer
					.parseInt(request.getParameter("match_id")));
			request.setAttribute("match", match);
		}
		return "manage/match_edit";
	}

	@RequestMapping(value = "/uploadFile")
	public String uploadFile(
			@RequestParam("file") CommonsMultipartFile file,
			HttpServletRequest req, int matchid) {
		
		//  download 目录下
		String name = "download";
		Match match = matchService.getMatch(matchid);
		match.setFile_url(fileService.handleUploadData(name, file));
		matchService.updateMatch(match);
		System.out.println("ss");
		return "manage/index";
	}

	@RequestMapping("/index.html")
	public String index(HttpServletRequest request) {
		List<Match> matchList = matchService.getAllMatch();
		request.setAttribute("matchList", matchList);
		return "manage/index";
	}

	@RequestMapping(value = "/api_editMatch", method = RequestMethod.POST)
	@ResponseBody
	public String addMatch(Match match, MatchContent match_content,
			HttpServletRequest request) {
		if (CommonUtil.isEmpty(match.getMatch_name())
				|| CommonUtil.isEmpty(match.getCover())) {
			return "{\"state\":104,\"comment\":\"请至少填写比赛名和封面图。\"}";
		} else {
			match_content.setMatch(match);
			match.setMatch_content(match_content);
			match.setState(0);
			match.setCreate_date(new Date(System.currentTimeMillis()));
			System.out.println(match.getCreate_date());
			System.out.println(	new Date(System.currentTimeMillis()));
		
			if (request.getParameter("match_id") != null) {
				matchService.updateMatch(match);
				return "{\"state\":200,\"comment\":\"保存成功。\"}";
			} else {
				if (matchService.addMatch(match) == true) {
					return "{\"state\":200,\"comment\":\"保存成功。\"}";
				} else {
					return "{\"state\":104,\"comment\":\"保存失败，请检查是否重复添加重名比赛。\"}";
				}
			}

		}
	}

	@RequestMapping(value = "api_deleteMatch")
	public String deleteMatch(@RequestParam("match_id") int match_id) {
		matchService.deleteMatch(match_id);
		return "redirect:index.html";
	}

	@RequestMapping(value = "/api_changeMatchState")
	public String changeMatchState(@RequestParam("state") int state,
			@RequestParam("match_id") int match_id) {
		matchService.changeMatchState(match_id, state);
		return "redirect:index.html";
	}

	@RequestMapping("/award.html")
	public String award(@RequestParam("match_id") int match_id,
			HttpServletRequest request) {
		Match match = matchService.getMatchWithSign(match_id);
		List<Prize> prizeList = match.getPrizes();
		request.setAttribute("match", match);
		request.setAttribute("prizeList", prizeList);
		return "manage/award";
	}
}
